package com.edu.controller;

import com.edu.bo.PaperGengerateVO;
import com.edu.entity.ScoreDetail;
import com.edu.service.PaperService;
import com.edu.service.ScoreDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/Exam")
public class ExamController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private ScoreDetailService scoreDetailService;

    /*
     * {1}前台弹出测试(考试)页面
     *    映射地址: /Exam/popPaper/206
     */
    @RequestMapping("/popPaper/{paperId}")
    public String popPaper(
            @PathVariable("paperId") Integer paperId,
            Model model
    ){
        //1.获取试卷预览信息
        PaperGengerateVO VO = paperService.previewPaper(paperId);
        //2.把试卷预览信息设置到模型域
        model.addAttribute("examPapersVO", VO);
        //3.内部转发到页面 /templates/front/examPaper.html
        return "front/examPaper";
    }

    /*
     * {2}保存学生考试记录
     *    映射地址: /Exam/stu/examPaper
     */
    @RequestMapping("/stu/examPaper")
    @ResponseBody
    public R examPaper(
            @RequestBody List<ScoreDetail> scoreDetails
            ){
        try {
            scoreDetailService.savePaperTestRecord(scoreDetails);
            return R.ok();
        }catch (Exception e){
            return R.error(e);
        }
    }

}